from typing import List

from pydantic import BaseModel, Field, root_validator

from ..common.trd_level import User

"""
Belong:Q&A问答相关
"""


class QuestionAndAnswerKeyword(BaseModel):
    """
    Q&A问答关键字
    """
    id: int = Field(None, description='关键字ID')
    name: str = Field(None, description='名称')

    @root_validator(pre=True)
    def create(cls, values):
        mdl = values.get('__from_odoo')
        env = values.get('__env')
        if mdl and env:
            values['id'] = mdl.id
            values['name'] = mdl.name
        return values


class QuestionAndAnswerLevel(BaseModel):
    """
    Q&A问答等级
    """
    id: int = Field(None, description='ID')
    name: str = Field(None, description='问答级别')
    is_root: bool = Field(None, description='是否为根')

    @root_validator(pre=True)
    def create(cls, values):
        mdl = values.get('__from_odoo')
        env = values.get('__env')
        if mdl and env:
            values['id'] = mdl.id
            values['name'] = mdl.name
            values['is_root'] = mdl.is_root
        return values


class QuestionAndAnswerLevelOneBase(BaseModel):
    """
    Q&A问答第一级别基础数据
    """
    id: int = Field(None, description='Q&A的记录ID')
    cover_url: str = Field(None, description='封面URL')
    title: str = Field(None, description='标题')
    introduction: str = Field(None, description='简介')

    @root_validator(pre=True)
    def create(cls, values):
        mdl = values.get('__from_odoo')
        env = values.get('__env')
        if mdl and env:
            values['id'] = mdl.id
            values['cover_url'] = (env['ir.attachment'].search([('res_id', '=', mdl.id), ('res_field', '=', 'cover')], limit=1)).url
            values['title'] = mdl.title
            values['introduction'] = mdl.introduction
        return values


class QuestionAndAnswerLevelOneDetail(BaseModel):
    """
    Q&A第一级别文章详情数据
    """

    id: int = Field(None, description='ID')
    code: str = Field(None, description='编码')
    question_answer_level: QuestionAndAnswerLevel = Field(None, description='级别')
    question_answer_types: List[str] = Field(None, description='类型')
    cover_url: str = Field(None, description='封面图片')
    title: str = Field(None, description='标题')
    sub_title: str = Field(None, description='副标题')
    introduction: str = Field(None, description='简介')
    body: str = Field(None, description='内容')
    author: User = Field(None, description='作者')
    publish_time: str = Field(None, description='发布时间')
    related_parent_question_and_answer: List[QuestionAndAnswerLevelOneBase] = Field(None, description='祖先Q&A问答')
    related_question_and_answers: List[QuestionAndAnswerLevelOneBase] = Field(None, description='关联的Q&A问答')
    related_next_question_and_answers: List[QuestionAndAnswerLevelOneBase] = Field(None, description='关联的下级Q&A问答')

    @root_validator(pre=True)
    def create(cls, values):
        def get_parent_question_and_answers(record, env):
            before_question_and_answer_id = record.rp_question_and_answer_id
            res_list = []
            while before_question_and_answer_id:
                question_and_answer_id = QuestionAndAnswerLevelOneBase(__from_odoo=before_question_and_answer_id, __env=env)
                res_list.append(question_and_answer_id)
                before_question_and_answer_id = before_question_and_answer_id.rp_question_and_answer_id
            return res_list

        mdl = values.get('__from_odoo')
        env = values.get('__env')
        if mdl and env:
            values['id'] = mdl.id
            values['code'] = mdl.code
            values['question_answer_level'] = QuestionAndAnswerLevel(__from_odoo=mdl.question_answer_level_id, __env=env)
            values['question_answer_types'] = [record.name for record in mdl.question_answer_type_ids]
            values['cover_url'] = (env['ir.attachment'].search([('res_id', '=', mdl.id), ('res_field', '=', 'cover')], limit=1)).url
            values['title'] = mdl.title
            values['sub_title'] = mdl.sub_title
            values['introduction'] = mdl.introduction
            values['body'] = mdl.body
            values['author'] = User(__from_odoo=mdl.author, __env=env)
            values['publish_time'] = mdl.publish_time.strftime('%Y-%m-%d %H:%M:%S')
            values['related_question_and_answers'] = [QuestionAndAnswerLevelOneBase(__from_odoo=record, __env=env) for record in mdl.related_question_and_answer_ids]
            values['related_parent_question_and_answer'] = get_parent_question_and_answers(mdl, env)
            values['related_next_question_and_answers'] = [QuestionAndAnswerLevelOneBase(__from_odoo=record, __env=env) for record in mdl.correlation_next_level_ids]
        return values
